import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default({
  namespaced:true,
  state: {
      token:window.localStorage.getItem('token') == null||undefined ? '' : window.localStorage.getItem('token' || '[]'),
      isOnline:window.localStorage.getItem('user'|| '[]') != null ,
      username: window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).username
  },
  mutations: {
    login (state, obj) {
      state.username = obj.user.username;
      state.token=obj.token;
      window.localStorage.setItem('token', obj.token)
      window.localStorage.setItem('user', JSON.stringify(obj.user))
      state.isOnline=true;
    },
    logout(state) {
      state.isOnline=false;
      window.localStorage.removeItem('user');
      window.localStorage.removeItem('token');
    }
  }
})



